/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Telas;

import SYSGEST.br.Entrada.Entrada;
import SYSGEST.br.Entrada.EntradaDAO;
import SYSGEST.br.Produto.Produtos;
import SYSGEST.br.Produto.ProdutosDAO;
import SYSGEST.conexao.ConectioFactory;
import java.sql.Connection;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.view.JasperViewer;

/**
 *
 * @author Mesopotanea
 */
public class RelEntrada extends javax.swing.JDialog {

    /**
     * Creates new form RelEntrada
     */
    ProdutosDAO prd = new ProdutosDAO();
    EntradaDAO entradadao = new EntradaDAO();

    public RelEntrada() {
        initComponents();
        setModal(true);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jcTipo = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        edtFunProd = new javax.swing.JFormattedTextField();
        btGerarRel = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        edtNome = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Relatório de Entrada de Produtos");

        jPanel7.setBackground(new java.awt.Color(0, 0, 153));

        jLabel7.setFont(new java.awt.Font("Verdana", 1, 24)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Relatório de Entrada Produtos");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                .addContainerGap(47, Short.MAX_VALUE)
                .addComponent(jLabel7)
                .addGap(45, 45, 45))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel7)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jcTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--", "GERAL", "NOME DO FUNCIONARIO", "CODIGO DO PRODUTO" }));

        jLabel3.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jLabel3.setText("Tipo.:");

        jLabel1.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jLabel1.setText("Produto.: ");

        try {
            edtFunProd.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#############")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        edtFunProd.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        edtFunProd.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        edtFunProd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edtFunProdActionPerformed(evt);
            }
        });

        btGerarRel.setFont(new java.awt.Font("Verdana", 0, 14)); // NOI18N
        btGerarRel.setText("Gerar Relatório");
        btGerarRel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btGerarRelActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N
        jLabel2.setText("Funcionário.:");

        edtNome.setFont(new java.awt.Font("Verdana", 0, 18)); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jcTipo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(edtFunProd, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
                    .addComponent(edtNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btGerarRel)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jcTipo))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(edtFunProd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(btGerarRel)
                    .addComponent(edtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btGerarRelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btGerarRelActionPerformed
        // TODO add your handling code here:
        if (jcTipo.getSelectedItem() == "NOME DO FUNCIONARIO") {
            ChamarRelatorioComFiltroNome();
        } else {
            if (jcTipo.getSelectedItem() == "CODIGO DO PRODUTO") {

                ChamarRelatorioComFiltroProd();
            } else {
                if (jcTipo.getSelectedItem() == "GERAL") {
//                    ChamarRelatorioGeral();

                } else {
                    JOptionPane.showMessageDialog(rootPane, "Selecione o Tipo de Relatório");
                }
            }
        }

    }
  /*  public void ChamarRelatorioGeral() {
        JasperReport pathjrxml;
        HashMap parametros = new HashMap();

        String sql = "", texto = "";

        Connection connection = new ConectioFactory().getConnection();
        try {

            JDialog viewer = new JDialog(new javax.swing.JFrame(), "Visualizar Entradas de Produtos", true);
            viewer.setSize(1000, 600);
            viewer.setLocationRelativeTo(null);
            viewer.setModal(true);

            pathjrxml = JasperCompileManager.compileReport("src/Relatorios/RelEntradaProdutos.jrxml");
            JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                    connection);
            JasperViewer jv = new JasperViewer(printReport, false);
            viewer.getContentPane().add(jv.getContentPane());
            viewer.setVisible(true);
            
        } catch (JRException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }*/

    public void ChamarRelatorioComFiltroProd() {
        JasperReport pathjrxml;
        HashMap parametros = new HashMap();

        String sql = "", texto = "";
        if (!edtFunProd.getText().equals("")) {
            Produtos l = prd.PesquisarProduto((edtFunProd.getText()));
            if (l == null) {
                JOptionPane.showMessageDialog(rootPane, "Funcionário não cadastrado!");
            } else {
                sql += "entradas.produ_codigo=" + l.getCodigo();
                parametros.put("texto", sql);

                Connection connection = new ConectioFactory().getConnection();
                try {

                    JDialog viewer = new JDialog(new javax.swing.JFrame(), "Entradas de Produtos", true);
                    viewer.setSize(1000, 600);
                    viewer.setLocationRelativeTo(null);
                    viewer.setModal(true);

                    pathjrxml = JasperCompileManager.compileReport("src/Relatorios/RelEntradaProdutos.ComFiltroFunc.jrxml");
                    JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                            connection);
                    JasperViewer jv = new JasperViewer(printReport, false);
                    viewer.getContentPane().add(jv.getContentPane());
                    viewer.setVisible(true);
                    //            JasperExportManager.exportReportToPdfFile(printReport, "src/Relatorio/Relatorio Saida De Produto.pdf");
                    //
                    //            jv.setVisible(true);
                } catch (JRException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void ChamarRelatorioComFiltroNome() {
        JasperReport pathjrxml;
        HashMap parametros = new HashMap();

        String sql = "", texto = "";
        if (!edtNome.getText().equals("")) {
            Entrada l = entradadao.pesquisarFuncionario(edtNome.getText());
            if (l == null) {
                JOptionPane.showMessageDialog(rootPane, "Funcionário não cadastrado!");
            } else {
                sql += "entradas.ID=" + l.getID();
                parametros.put("texto", sql);

                Connection connection = new ConectioFactory().getConnection();
                try {

                    JDialog viewer = new JDialog(new javax.swing.JFrame(), "Entradas de Produtos", true);
                    viewer.setSize(1000, 600);
                    viewer.setLocationRelativeTo(null);
                    viewer.setModal(true);

                    pathjrxml = JasperCompileManager.compileReport("src/Relatorios/RelEntradaProdutos.ComFiltroFunc.jrxml");
                    JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                            connection);
                    JasperViewer jv = new JasperViewer(printReport, false);
                    viewer.getContentPane().add(jv.getContentPane());
                    viewer.setVisible(true);
                    //            JasperExportManager.exportReportToPdfFile(printReport, "src/Relatorio/Relatorio Saida De Produto.pdf");
                    //
                    //            jv.setVisible(true);
                } catch (JRException ex) {
                    Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    public void ChamaRelatorioSemFIltro() {
        JasperReport pathjrxml;
        HashMap parametros = new HashMap();

        String sql = "", texto = "";

        Connection connection = new ConectioFactory().getConnection();
        try {

            JDialog viewer = new JDialog(new javax.swing.JFrame(), "Visualizar Entradas", true);
            viewer.setSize(1000, 600);
            viewer.setLocationRelativeTo(null);
            viewer.setModal(true);

            pathjrxml = JasperCompileManager.compileReport("src/Relatorios/RelEntradaProdutos.jrxml");
            JasperPrint printReport = JasperFillManager.fillReport(pathjrxml, parametros,
                    connection);
            JasperViewer jv = new JasperViewer(printReport, false);
            viewer.getContentPane().add(jv.getContentPane());
            viewer.setVisible(true);
            //            JasperExportManager.exportReportToPdfFile(printReport, "src/Relatorio/Relatorio Saida De Produto.pdf");
            //
            //            jv.setVisible(true);
        } catch (JRException ex) {
            Logger.getLogger(Principal.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RelSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RelSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RelSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RelSaida.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RelEntrada().setVisible(true);
            }
        });
    }//GEN-LAST:event_btGerarRelActionPerformed

    private void edtFunProdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edtFunProdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_edtFunProdActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btGerarRel;
    private javax.swing.JFormattedTextField edtFunProd;
    private javax.swing.JTextField edtNome;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JComboBox jcTipo;
    // End of variables declaration//GEN-END:variables
}
